package nc.bc.cmp.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import nc.bc.cmp.itf.IAttchementFileService;
import nc.bs.dao.BaseDAO;
import nc.bs.fileupload.itf.IBipFileUploadService;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.vo.cmp.bill.BillAggVO;
import nc.vo.cmp.bill.BillDetailVO;
import nc.vo.cmp.bill.BillVO;
import nc.vo.cmp.qdfkd.AggQdfkdVO;
import nc.vo.cmp.qdfkd.QdfkdVO;
import nc.vo.pub.BusinessException;

class AttchementFileServiceImpl implements IAttchementFileService{
	private BaseDAO dao;
	private IBipFileUploadService fileUploadService;

	public BaseDAO getDao() {
		return dao == null ? new BaseDAO() : dao;
	}

	public AttchementFileServiceImpl() {
	}

	@Override
	public void attachmentUploadFDK(BillAggVO[] aggvos) throws BusinessException {
		if(aggvos == null) return;
		for(BillAggVO vo : aggvos) {
			List<String> sqds = new ArrayList<String>();
			List<String> bxds = new ArrayList<String>();
			BillVO hvo = (BillVO)vo.getParentVO();
			BillDetailVO[] bvos = (BillDetailVO [] )vo.getChildrenVO();
			String pk_paybill = hvo.getPk_paybill();
			String vbillcode = hvo.getBill_no();
			String pk_group = hvo.getPk_group();
			String pk_org = hvo.getPk_org();
			String pk_billtypeid = hvo.getPk_billtypeid();
			String pk_org_v = hvo.getPk_org_v();
			String userid = hvo.getCreator();
			if(bvos != null)
			{
				Object def1 = hvo.getAttributeValue("def1"); // 申请单附件地址
				if(def1 != null) { // 申请单附件
					JSONArray files = JSONArray.parseArray(def1.toString());
					for(Object filepath : files) {
						sqds.add(filepath.toString());
					}
				}
				for(BillDetailVO bvo : bvos) {
					Object bxdef1 = bvo.getAttributeValue("def1");// 报销单附件地址
					if(bxdef1 != null ) {
						JSONArray items = JSONArray.parseArray(bxdef1.toString());
						for(Object item : items) {
							bxds.add(item.toString());
						}
					}
				}
			}
			if(sqds != null && !sqds.isEmpty()) {
				this.uploadAttchement("申请单附件", vbillcode, pk_group, pk_org, pk_org_v, pk_billtypeid, sqds, pk_paybill, userid);
			}
			if(bxds != null && !bxds.isEmpty()) {
				this.uploadAttchement("报销单附件", vbillcode, pk_group, pk_org, pk_org_v, pk_billtypeid, sqds, pk_paybill, userid);
			}
		}
	}

	@Override
	public void attachmentUploadQDFK(AggQdfkdVO[] aggvos) throws BusinessException {
		if(aggvos == null) return;
		for(AggQdfkdVO vo : aggvos) {
			List<String> sqds = new ArrayList<String>();
			List<String> bxds = new ArrayList<String>();
			QdfkdVO hvo = vo.getParentVO();
			BillDetailVO[] bvos = (BillDetailVO [] )vo.getChildrenVO();
			String pk_paybill = hvo.getPk_sfid();
			String vbillcode = hvo.getBillno();
			String pk_group = hvo.getPk_group();
			String pk_org = hvo.getPk_org();
			String pk_billtypeid = hvo.getTranstypepk();
			String pk_org_v = hvo.getPk_org_v();
			String userid = hvo.getCreator();
			if(bvos != null)
			{
				Object def1 = hvo.getAttributeValue("def1"); // 申请单附件地址
				if(def1 != null) { // 申请单附件
					String[] files = def1.toString().split(",");
                    Collections.addAll(sqds, files);
				}
			}
			if(sqds != null && !sqds.isEmpty()) {
				String fullPath = pk_paybill+"/确定付款单";
				getFileUploadService().fileUploadNC(fullPath, pk_paybill, vbillcode, sqds, pk_group, pk_org, pk_org_v, pk_billtypeid,userid);

			}
		}
	}

	/**
	 * @param fileName        附件文件夹名称
	 * @param vbillcode       单据号
	 * @param pk_group        集团
	 * @param pk_org          组织
	 * @param pk_org_v        组织版本
	 * @param pk_billtypeid   单据类型
	 * @param attchmentsurls
	 * @param pk_paybill
	 * @param userid
	 * @throws BusinessException
	 */
	protected void uploadAttchement(String fileName,String vbillcode, String pk_group,String pk_org, String pk_org_v,String pk_billtypeid, List<String> attchmentsurls, String pk_paybill, String userid) throws BusinessException {
		IFileSystemService fservice = NCLocator.getInstance().lookup(IFileSystemService.class);
		fservice.createNewFileNode(pk_paybill, fileName, userid);
		String fullPath = pk_paybill+"/"+fileName;
		getFileUploadService().fileUploadURL(fullPath, pk_paybill, vbillcode, attchmentsurls, pk_group, pk_org, pk_org_v, pk_billtypeid,userid);
	}
	public IBipFileUploadService getFileUploadService() {
		if(fileUploadService == null) {
			fileUploadService = NCLocator.getInstance().lookup(IBipFileUploadService.class);
		}
		return fileUploadService;
	}
}
